import Mock from 'mockjs'
import { createRes } from './common'
import * as T from '@/api/assetManagement/assetManagement_type'
import { ApiPath } from '@/api/assetManagement/assetManagement'

const basePath = import.meta.env.VITE_API_BASE_URL

// 通过Mock生成资源详情
export function MockTResourceItem(): T.TResourceItem {
  return Mock.mock({
    id: '@id',
    code: '@string',
    create: '@datetime',
    created_time: '@datetime',
    data_browsing_number: '@integer(1, 40)',
    data_call_number: '@integer(1, 40)',
    data_download_number: '@integer(1, 40)',
    data_fields_count: '@integer(1, 40)',
    data_update_time: '@datetime',
    from: '@string',
    // is_root: 0 | 1,
    is_root: '@integer(0, 1)',
    name: '@ctitle',
    production_resource: '@string',
    "resource_owner|1-5": [
      {
        address: '@string',
        code: '@string',
        created_time: '@datetime',
        credit_code: '@string',
        id: '@id',
        name: '@ctitle'
      }
    ],
    scene: ['@string'],
    security_level: '@string',
    sync_consanguinity: '@boolean' ,
    update_frequency: '@string',
    update_time: '@datetime',
    version: '@integer(1,20)'
  })
}

// 通过Mock生成资源列表
export function MockTResourceList(): T.TResourceList {
  return Mock.mock({
    'list|10-20': [MockTResourceItem()]
  }).list
}

Mock.mock(new RegExp(`${basePath}${ApiPath.List}.*`,'gi'), 'get', createRes(MockTResourceList()));

// 创建资源
const mockCreateResource: any = Mock.mock('id: @integer(1, 9999)')
Mock.mock(new RegExp(`${basePath}${ApiPath.Create}.*`,'gi'), 'post', createRes(mockCreateResource.id));

// 获取资源详情
const mockGetResource: any = MockTResourceItem()
Mock.mock(new RegExp(`${basePath}${ApiPath.Get}.*`,'gi'), 'get', createRes(mockGetResource));

// 获取资源关系
function MockTResourceRelationItem(): T.TResourceRelationItem {
  return Mock.mock({
    currently: '@boolean',
    id: '@id',
    name: '@ctitle',
    parent_id: ['@id'],
    type: '@string',
    version: '@integer(1,20)'
  })
}
const mockResourceRelation: any = Mock.mock({
  'list|1-5': [MockTResourceRelationItem()]
}).list
Mock.mock(new RegExp(`${basePath}${ApiPath.GetRelation}.*`,'gi'), 'get', createRes([
  {
    currently: false,
    id: '资源:22',
    name: '公司财务报表(V2)',
    parent_id: ['资源:43'],
    type: '根资源',
    version: 2
  },
  {
    currently: false,
    id: '资源:43',
    name: '环境监测数据(V2)',
    parent_id: ['资源:26'],
    type: '普通资源',
    version: 2
  }
]));

// 资源版本
function MockTResourceVerItem(): T.TResourceVerItem {
  return Mock.mock({
    create_name: '@cname',
    create_time: '@datetime',
    id: '@id',
    update_time: '@datetime',
    version: '@integer(1,20)'
  })
}
const mockResourceVers: any = Mock.mock({
  'list|10-20': [MockTResourceVerItem()]
}).list
Mock.mock(new RegExp(`${basePath}${ApiPath.Vers}.*`,'gi'), 'get', createRes(mockResourceVers));

console.log('mock: assetManagement =>', 'mocked!', basePath)